package cloud.zhixinglu.www.spring.boot.mybatis.plus.configure;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 *	数据库创建和更新自动填充数据
 *
 * @author weixin
 * */
@Log4j2
@Configuration
public class MyMetaObjectHandler implements MetaObjectHandler {

	private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";

    @Override
    public void insertFill(MetaObject metaObject) {
        log.debug("start insert fill ....");
        log.info("元注解时间为"+metaObject);
        //避免使用metaObject.setValue()
        // 创建时间
        this.setFieldValByName("createTime",  LocalDateTime.now().format(DateTimeFormatter.ofPattern(FORMAT)), metaObject);

        this.updateFill(metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.debug("start update fill ....");
        this.setFieldValByName("updateTime",  LocalDateTime.now().format(DateTimeFormatter.ofPattern(FORMAT)), metaObject);
    }

}
